<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="filter-container">
        <el-input placeholder="工单标题" class="filter-item search-item"
                  v-model="queryParams.model.title"/>
        <el-select placeholder="当前处理人" class="filter-item search-item"
                   v-model="queryParams.model.processor" :value="users"/>
        <el-select v-model="queryParams.model.priority" placeholder="请选择优先级" clearable style="width: 130px" @change="getList">
          <el-option label="一般" :value="1" />
          <el-option label="紧急" :value="2" />
          <el-option label="非常紧急" :value="3" />
        </el-select>
        <el-select v-model="queryParams.model.isEnd" placeholder="请选择优先级" clearable style="width: 130px" @change="getList">
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
        <el-date-picker
          v-model="queryParams.model.createTime"
          clearable
          type="datetimerange"
          picker-options=""
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
          @change="getList"
        />
        <el-button @click="getList" class="filter-item" plain type="primary">{{ $t('table.search') }}</el-button>
        <el-button @click="reset" class="filter-item" plain type="warning">{{ $t('table.reset') }}</el-button>
      </div>

      <el-table
        :data="tableData.records"
        :key="tableKey"
        @cell-click="cellClick"
        @filter-change="filterChange"
        @selection-change="onSelectChange"
        @sort-change="sortChange"
        border fit row-key="id"
        ref="table"
        style="width: 100%;"
        v-loading="loading"
      >
        <el-table-column label="标题" :show-overflow-tooltip="true" sortable="title" align="left"
                         min-width="120px" prop="title">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
          </template>
        </el-table-column>

        <el-table-column label="流程" :show-overflow-tooltip="true" sortable="process_name" align="left"
                         min-width="120px" prop="process_name">
          <template slot-scope="scope">
            <span>{{ scope.row.process_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当前状态" :show-overflow-tooltip="true" sortable="state_name" align="left"
                         min-width="120px" prop="state_name">
          <template slot-scope="scope">
            <span>{{ scope.row.state_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当前处理人" :show-overflow-tooltip="true" sortable="principals" align="left"
                         min-width="120px" prop="principals">
          <template slot-scope="scope">
            <span v-if="scope.row.isEnd===0">{{ scope.row.principals }}</span>
          </template>
        </el-table-column>
        <el-table-column label="优先级" :show-overflow-tooltip="true" width="120" align="left">
          <template slot-scope="scope">
            <span v-if="scope.row.priority===2">
              <el-tag type="warning">紧急</el-tag>
            </span>
            <span v-else-if="scope.row.priority===3">
              <el-tag type="danger">非常紧急</el-tag>
            </span>
            <span v-else>
              <el-tag type="success">一般</el-tag>
            </span>
          </template>
        </el-table-column>

        <el-table-column label="是否结束" :show-overflow-tooltip="true" width="80" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isEnd===0" size="mini" type="success">否</el-tag>
            <el-tag v-else size="mini" type="danger">是</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="create_time" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>

        <el-table-column label="创建人" align="center" prop="createName" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.createName }}</span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleView(scope.row)"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :limit.sync="queryParams.size"
        :page.sync="queryParams.current" :total="Number(tableData.total)"
        @pagination="fetch" v-show="tableData.total>0"/>
    </el-card>
    <handle-preview
      :visible="handle.isVisible"
      @close="close"
      @success="success"
      ref="handlePreview"
    />


  </div>
</template>

<script>
  import Vue from 'vue'
  import {
    GenerateForm
  } from '@/components/VueFormMaking'
  import 'form-making/dist/FormMaking.css'
  Vue.component(GenerateForm.name, GenerateForm)
  import handlePreview from './components/search/handle'
  import workOrderApi from '@/api/process/work-order'
import elDragDialog from '@/directive/el-drag-dialog'
import Pagination from '@/components/Pagination'
import {downloadFile, initQueryParams} from '@/utils/commons'
// 搜索

export default {
  components: { Pagination,elDragDialog ,downloadFile,handlePreview },
  data() {
    return {
      handle: {
        isVisible: false,
        type: "add"
      },
      users: [],
      nodeList: [],
      dialogVisible: false,
      queryParams: {},
      total: 0,
      loading: false,
      ticketList: [],
      queryParams: initQueryParams({
        model: {}
      }),
      selection: [],
      tableKey: 0,
      tableData: {
        total: 0
      },
      ruleForm: {
        work_order_id: '',
        node_id: '',
        user_id: '',
        remarks: ''
      },
      rules: {
        node_id: [
          { required: true, message: '请选择节点', trigger: 'change' }
        ],
        user_id: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    close() {
      this.handle.isVisible = false;
    },
    success(){
      this.getList();
    },
    openHandlePreview(row) {
      this.handle.type = "add";
      this.handle.isVisible = true;
      this.$refs.handlePreview.setHandleView(row);
    },
    reset() {
      // 先清除校验，再清除表单，不然有奇怪的bug
      this.queryParams = initQueryParams({
        model: {}
      });
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
    },
    getList() {
      this.fetch();
    },
    handleSearch(val) {
      for (var k in val) {
        this.listQuery[k] = val[k]
      }
      this.getList()
    },
    handleView(row) {
      this.openHandlePreview( { workOrderId: row.id, processId: row.process })
      // this.$router.push({path: '/process/list/handle', query: { workOrderId: row.id, processId: row.process }})
      // this.$router.push({ name: 'ProcessListHandle', query: { workOrderId: row.id, processId: row.process }})
    },
    handleSelectionChange() {}
    ,
    fetch(params = {}) {
      this.loading = true;
      if (this.queryParams.timeRange) {
        this.queryParams.extra.createTime_st = this.queryParams.timeRange[0];
        this.queryParams.extra.createTime_ed = this.queryParams.timeRange[1];
      }
      this.queryParams.current = params.current ? params.current : this.queryParams.current;
      this.queryParams.size = params.size ? params.size : this.queryParams.size;
      this.queryParams.model.classify = 3
      workOrderApi.workOrderList(this.queryParams).then(response => {
        const res = response.data;
        if (res.isSuccess) {
          this.tableData = res.data;
        }
        this.loading = false
      }).finally(() => this.loading = false);
    },
    search() {
      this.fetch({
        ...this.queryParams,
      })
    },
    reset() {
      this.queryParams = initQueryParams({
        model: {}
      });
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    sortChange(val) {
      this.queryParams.sort = val.prop;
      this.queryParams.order = val.order;
      if (this.queryParams.sort) {
        this.search();
      }
    },
    filterChange(filters) {
      for (const key in filters) {
        if (key.includes('.')) {
          const val = {};
          val[key.split('.')[1]] = filters[key][0];
          this.queryParams.model[key.split('.')[0]] = val;
        } else {
          this.queryParams.model[key] = filters[key][0]
        }
      }
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    cellClick(row, column) {
      if (column['columnKey'] === "operation") {
        return;
      }
      if (column['columnKey'] === "clientId") {
        copy(row[column.property]);
        this.$message({
          message: this.$t('tips.copySelected'),
          type: 'success'
        })
        return;
      }

      let flag = false;
      this.selection.forEach((item) => {
        if (item.id === row.id) {
          flag = true;
          this.$refs.table.toggleRowSelection(row);
        }
      })
      if (!flag) {
        this.$refs.table.toggleRowSelection(row, true);
      }
    },
  }
}
</script>

<style scoped>

</style>
